using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.Pool;
using UnityEngine.EventSystems;
public class ShowBuffInfo : MonoBehaviour
{
    private static class FrameColor
    {
        public static readonly Color Positive = new Color(99f / 255f, 174f / 255f, 198f / 255f, 1);
        public static readonly Color Negative = new Color(181f / 255f, 81f / 255f, 82f / 255f, 1);
        public static readonly Color Neutral = new Color(0.5f, 0.5f, 0.5f, 1);
    }
    [SerializeField]
    private Image m_BuffIcon;
    [SerializeField]
    private Image m_Frame;
    [SerializeField]
    private Image m_Mask;
    [SerializeField]
    private TextMeshProUGUI m_BuffHeap;
    [SerializeField]
    private GameObject m_Lines;
    [SerializeField]
    private RectTransform m_RollLine;

    private BuffBase<PlayerController> m_Buff;
    private ObjectPool<ShowBuffInfo> m_RecyclePool;
    private bool m_NeedShowHeap;
    private bool m_NeedTimer;
    private InfoSource m_InfoSource;
    #region Public方法
    public void Init(BuffBase<PlayerController> buff, ObjectPool<ShowBuffInfo> recyclePool)
    {
        m_Buff = buff;
        m_RecyclePool = recyclePool;

        m_InfoSource.icon = buff.BuffData.iconPath;
        m_InfoSource.head = buff.BuffData.buffName;
        m_InfoSource.body = buff.GetDescription();
        m_InfoSource.foot = "来自:" + buff.Provider;





        m_BuffIcon.sprite = Resources.Load<Sprite>(buff.BuffData.iconPath);
        if (buff.BuffData.maxLevel > 1)
        {
            m_NeedShowHeap = true;
            m_BuffHeap.gameObject.SetActive(true);
        }
        else
        {
            m_NeedShowHeap = false;
            m_BuffHeap.gameObject.SetActive(false);
        }

        if (!buff.BuffData.isPermanent)
        {
            m_NeedTimer = true;
            m_Mask.gameObject.SetActive(true);
            m_Lines.SetActive(true);
        }
        else
        {
            m_NeedTimer = false;
            m_Mask.gameObject.SetActive(false);
            m_Lines.SetActive(false);
        }
        switch (buff.BuffData.buffType)
        {
            case BuffType.Negative:
                m_Frame.color = FrameColor.Negative;
                break;
            case BuffType.Positive:
                m_Frame.color = FrameColor.Positive;
                break;
            case BuffType.Neutral:
                m_Frame.color = FrameColor.Neutral;
                break;
            default:
                break;
        }
    }
    #endregion

    #region Private方法

    #endregion

    #region Unity方法
    private void Awake()
    {
        m_InfoSource = GetComponent<InfoSource>();
    }
    private void Update()
    {
        if (m_Buff == null) return;

        if (m_Buff.CurrentLevel == 0)
        {
            m_Buff = null;
            m_RecyclePool.Release(this);
            return;
        }
        if (m_NeedShowHeap)
        {
            m_BuffHeap.text = m_Buff.CurrentLevel.ToString();
        }
        if (m_NeedTimer)
        {
            m_Mask.fillAmount = 1 - (m_Buff.ResidualDuration / m_Buff.BuffData.maxDuration);
            m_RollLine.eulerAngles = new Vector3(0, 0, m_Mask.fillAmount * -360);
        }

    }
    #endregion
}
